package com.safe.ly_safe.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import com.safe.ly_safe.R;
import com.safe.ly_safe.base.BaseActivity;
import com.safe.ly_safe.control.view.MainView;
import com.safe.ly_safe.entity.SdpNameBean;
import com.safe.ly_safe.utils.SdpSelectPopup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @PACKAGE :com.safe.ly_safe.activity
 * @Author : HSF
 * @Time : On 2022/4/24 16:12
 * @Description : LoginActivity
 * 登录页
 */
public class LoginActivity extends BaseActivity implements MainView {

    @BindView(R.id.setIv)
    ImageView setIv;

    @BindView(R.id.rightIv)
    ImageView rightIv;

    @BindView(R.id.sdpEdit)
    EditText sdpEdit;

    @BindView(R.id.nameEdit)
    EditText nameEdit;

    @BindView(R.id.passEdit)
    EditText passEdit;

    @BindView(R.id.loginBtn)
    AppCompatButton loginBnt;

    @BindView(R.id.sdpLine)
    View sdpLine;

    private SdpSelectPopup sdpSelectPopup;

    private List<SdpNameBean> sdpNameList = new ArrayList<>();

    private int GO_SET_UP_CODE = 1;

    @Override
    protected void setFullScreen() {
        //隐藏标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    protected String loadingMsg() {
        return "正在登录,请稍候...";
    }

    @Override
    protected void initView() {

        SdpNameBean sdpNameBean0 = new SdpNameBean();
        sdpNameBean0.setName("LSKDE010203");
        sdpNameBean0.setCheck(false);

        SdpNameBean sdpNameBean1 = new SdpNameBean();
        sdpNameBean1.setName("HRWETWF0365");
        sdpNameBean1.setCheck(false);

        SdpNameBean sdpNameBean2 = new SdpNameBean();
        sdpNameBean2.setName("TYRERTEF654632121SDSDF");
        sdpNameBean2.setCheck(false);

        sdpNameList.add(sdpNameBean0);
        sdpNameList.add(sdpNameBean1);
        sdpNameList.add(sdpNameBean2);
    }

    @Override
    public void showLoading(int type) {
        mDialog.show();
    }

    @Override
    public void hideLoading(int type) {
        mDialog.dismiss();
    }

    @Override
    public void onSuccess(int type, Object result) {

    }

    @Override
    public void onError(int type, Throwable e) {

    }

    @SuppressLint("NonConstantResourceId")
    @OnClick({R.id.setIv,R.id.rightIv,R.id.loginBtn})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.setIv:
                //设置
                if(sdpSelectPopup != null && sdpSelectPopup.isShowing()){
                    sdpSelectPopup.dismiss();
                }
                startActivityForResult(new Intent(this, ServerListActivity.class),GO_SET_UP_CODE);
                break;
            case R.id.rightIv:
                //sdp下拉
                if(sdpSelectPopup == null){
                    sdpSelectPopup = new SdpSelectPopup(this,sdpNameList);
                }
                sdpSelectPopup.setOnSdpClickListener(new SdpSelectPopup.OnSdpSelectItemListener() {
                    @Override
                    public void onSdpPopupClick(String selectName) {
                        sdpEdit.setText(selectName);
                    }
                });
                if(!sdpSelectPopup.isShowing()){
                    sdpSelectPopup.showPopWindow(sdpLine);
                }else{
                    sdpSelectPopup.dismiss();
                }
                break;
            case R.id.loginBtn:
                //登录
                startActivity(new Intent(this,MainActivity.class));
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data != null){
            if(requestCode == GO_SET_UP_CODE && resultCode == RESULT_OK){
                String serverUrl = data.getStringExtra("serverUrl");
                Toast.makeText(this,"serverUrl::"+serverUrl,Toast.LENGTH_SHORT).show();
            }
        }
    }
}
